package com.k2data.k2app.common;

import com.k2data.k2app.constant.Constant;
import com.k2data.k2app.domain.po.TaskDTO;
import com.k2data.k2app.domain.po.JobTaskDO;
import com.k2data.k2app.exception.K2ResponseException;
import com.k2data.k2app.rest.domain.RectObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 各种参数检查, 不通过抛异常
 *
 * @author lidong9144@163.com 17-3-29.
 */
@Component
public class K2Assert {
    private static Validator validator;

    @Autowired
    public K2Assert(Validator validator) {
        K2Assert.validator = validator;
    }


    /**
     * 检查 componentId 对应的数据是否存在, 同时判断是否等于删除关联表标志
     *
     * @param taskId taskId
     * @throws K2ResponseException componentId 对应的数据不存在并且不等于删除关联表标志
     */
    public static void existRelationTaskId(Long taskId) throws K2ResponseException {
        if (!Constant.DELETE_TABLE_RELATION.equals(taskId) && !validator.checkTaskID(taskId)) {
            throw new K2ResponseException(ResponseConstant.ID_NOT_FOUND,
                    String.format("%s(id: %s)", ResponseConstant.MSG_TASK_ID_NOT_FOUND, taskId));
        }
    }


    /**
     * 检查 是否存在相同的任务
     * 名称,模型,设备,频率
     *
     * @param taskDO
     * @throws K2ResponseException
     */
    public static void existTask(TaskDTO taskDO) throws K2ResponseException {
        if (validator.checkTask(taskDO)) {
            throw new K2ResponseException(ResponseConstant.TASK_IS_EXIST,
                    String.format("%s(name: %s)", ResponseConstant.MSG_TASK_IS_EXIST, taskDO.getTaskName()));
        }
    }

    /**
     * 检查 任务是否正在运行
     *
     * @param jobTaskDO
     * @throws K2ResponseException
     */
    public static void checkRunning(JobTaskDO jobTaskDO) throws K2ResponseException {
        if (String.valueOf(jobTaskDO.getStatus()).equals(String.valueOf(MyConstant.RUNNING))) {
            throw new K2ResponseException(ResponseConstant.TASK_IS_RUNNING,
                    String.format("任务(%s)正在运行，运行作业(%s)",jobTaskDO.getTaskId(), jobTaskDO.getJobId()));
        }
    }

    /**
     * 连接pas服务器是否出错
     *
     * @throws K2ResponseException
     */
    public static void connectionException(Exception e) throws K2ResponseException {
        throw new K2ResponseException(ResponseConstant.GENERAL,
                String.format("%s", e.getMessage()));
    }

    public static void connectionException(String msg,Exception e) throws K2ResponseException {
        throw new K2ResponseException(ResponseConstant.GENERAL,
                String.format("%s,%s", msg,e.getMessage()));
    }

    /**
     * 检查 从pas获取数据是否出错
     *
     * @param resultMap pas返回结果
     * @throws K2ResponseException
     */
    public static void isExceptionByPas(Map<String,Object> resultMap) throws K2ResponseException {
        if (resultMap.size() == 0) {
            throw new K2ResponseException(ResponseConstant.GENERAL,
                    String.format("%s","访问pas，返回结果为 null"));
        }
        if (resultMap.get("code") != null && "1".equals(resultMap.get("code").toString())) {
            throw new K2ResponseException(ResponseConstant.ID_NOT_FOUND,
                    String.format("%s", resultMap.get("message").toString()));
        }
    }
    public static void isExceptionByPas(RectObject rectObject) throws K2ResponseException {
        if (rectObject  == null) {
            throw new K2ResponseException(ResponseConstant.GENERAL,
                    String.format("%s","访问pas，返回结果为 null"));
        }
        if (rectObject.getCode() != null && "1".equals(rectObject.getCode().toString())) {
            throw new K2ResponseException(ResponseConstant.ID_NOT_FOUND,
                    String.format("%s", rectObject.getMessage().toString()));
        }
    }

    /**
     * 检查 是否匹配运行的条件
     * @return
     */
    public static void isCanRun(JobTaskDO jobTaskDOLatest, TaskDTO taskDO) throws K2ResponseException{
        LocalDateTime now = LocalDateTime.now();
        if(now.isBefore(taskDO.getStartDate())){
            throw new K2ResponseException(ResponseConstant.TASK_TIME_OVER,
                    String.format("%s",ResponseConstant.TIME_BEFORE_ERROR ));
        }
        if(jobTaskDOLatest != null) {
            checkRunning(jobTaskDOLatest);
            LocalDateTime jobEnd = jobTaskDOLatest.getEndTime();
            LocalDateTime taskEnd = taskDO.getEndDate();
            if(jobEnd.isAfter(taskEnd) || jobEnd.equals(taskEnd)){
                throw new K2ResponseException(ResponseConstant.TASK_TIME_OVER,
                        String.format("%s",ResponseConstant.TIME_OVER_ERROR ));
            }
        }
    }



    /**
     * 检查 是否匹配运行的条件
     * @return
     */
    public static void isUseful(TaskDTO taskDTO) throws K2ResponseException{
       if(!validator.checkIsUsed(taskDTO)){
            throw new K2ResponseException(ResponseConstant.TASK_UNUSEFUL,
                    String.format("%s",ResponseConstant.TASK_UNUSEFUL_ERROR ));
        }
    }
}
